<template>
  <div class="content-container">
    <el-row
      type="flex"
      justify="space-between"
      class="header-filter four-border-blueBg"
    >
      <div class="row-left">
        <TextInput
          v-model="listParams.roleName"
          class="item"
          clearable
          :placeholder="$t('userCenter.roleName')"
        />
        <el-button type="primary" @click="handleQuery">{{
          $t('public.search')
        }}</el-button>
        <el-button plain @click="handleReset">{{
          $t('public.reset')
        }}</el-button>
      </div>
      <div class="row-right">
        <el-button type="primary" @click="handleCreate"
          >+ {{ $t('public.create') }}</el-button
        >
      </div>
    </el-row>
    <div class="base-table-container">
      <BaseTable
        ref="refContentTable"
        v-loading="loading"
        :data="tableData"
        row-key="id"
      >
        <TableColumn
          prop="roleName"
          :label="$t('userCenter.roleName')"
          :column-width="columnWidthObj.roleName"
        />
        <!-- <TableColum
          prop="identifier"
          :label="$t('userCenter.roleId')"
          :column-width="columnWidthObj.identifier"
        /> -->
        <TableColumn
          prop="deptName"
          :label="$t('userCenter.belongDept')"
          :column-width="columnWidthObj.deptName"
        />
        <!-- <TableColum
          prop="roleType"
          :label="$t('userCenter.roleType')"
          :column-width="columnWidthObj.roleType"
        >
          <template #default="{ row }">
            <span>{{
              g_options.getOptionText(
                g_options.roleTypeOptions,
                row.roleType,
                '--'
              )
            }}</span>
          </template>
        </TableColumn> -->
        <TableColumn
          prop="roleDesc"
          :label="$t('public.desc')"
          :column-width="columnWidthObj.roleDesc"
        />
        <TableColumn
          prop="action"
          fixed="right"
          :label="$t('public.action')"
          :column-width="columnWidthObj.action"
        >
          <template #default="{ row }">
            <!-- <el-button text @click="handleDetailDialog(row)">
              {{ $t('public.detail') }}
            </el-button> -->
            <el-button
              :disabled="row.id * 1 === 1"
              text
              @click="handleEditDialog(row)"
            >
              {{ $t('public.edit') }}
            </el-button>
            <el-button
              v-if="row.roleType !== 1"
              text
              @click="handleDelete(row)"
              >{{ $t('public.delete') }}</el-button
            >
          </template>
        </TableColumn>
      </BaseTable>
      <BasePagination
        v-model:page-size="pageSize"
        v-model:current-page="pageNum"
        :total="total"
        @change="getTableList()"
      />
    </div>
    <!-- 新建/编辑 -->
    <AddOrEditDialog
      v-if="addOrEditVisible"
      :id="rowId"
      v-model="addOrEditVisible"
      :type="addOrEditType"
      @update="handleQuery"
    />
    <!-- 详情 -->
    <!-- <DetailDialog v-if="detailVisible" v-model="detailVisible" :id="rowId" /> -->
  </div>
</template>

<script setup>
import AddOrEditDialog from './component/AddOrEditDialog.vue'
// import DetailDialog from './component/DetailDialog.vue'

const listParams = ref({
  roleName: ''
})
const refContentTable = ref(null)
const loading = ref(false)
const tableData = ref([])
const columnWidthObj = ref({})
const total = ref(0)
const pageSize = ref(10)
const pageNum = ref(1)
const addOrEditVisible = ref(false)
const addOrEditType = ref('detail')
const rowId = ref('')
// const detailVisible = ref(false)

const getTableList = () => {
  loading.value = true
  tableData.value = []
  g_api.userCenter
    .apiRoleList({
      pageNum: pageNum.value,
      pageSize: pageSize.value,
      ...listParams.value
    })
    .then(({ code, data }) => {
      if (code === '200' && data) {
        tableData.value = data.items
        total.value = data.total
        useTableColumnWidth({
          refTable: refContentTable.value
        }).getTableColumnWidth(data => {
          columnWidthObj.value = data
        })
      }
    })
    .finally(() => {
      loading.value = false
    })
}

onMounted(() => {
  getTableList()
})

const handleQuery = () => {
  pageNum.value = 1
  getTableList()
}

const handleReset = () => {
  Object.assign(listParams.value, {
    roleName: ''
  })
  handleQuery()
}

const handleCreate = () => {
  addOrEditVisible.value = true
  addOrEditType.value = 'add'
}

// const handleDetailDialog = row => {
//   detailVisible.value = true
//   rowId.value = row.id
// }

const handleEditDialog = row => {
  addOrEditVisible.value = true
  addOrEditType.value = 'edit'
  rowId.value = row.id
}

const handleDelete = row => {
  g_utils
    .deleteConfirm()
    .then(() => {
      loading.value = true
      g_api.userCenter
        .apiRoleDelete({
          roleId: row.id
        })
        .then(res => {
          if (res.code === '200') {
            ElMessage.success(res.message)
            handleQuery()
          }
        })
        .finally(() => {
          loading.value = false
        })
    })
    .catch(() => {})
}
</script>

<style lang="scss" scoped></style>
